package com.zhanbo.zhanbowymusic.main;

import android.os.Bundle;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.mwx.baselib.base.BaseFragment;
import com.mwx.baselib.bean.PlayListSongBean;
import com.mwx.baselib.bean.SongBean;
import com.mwx.baselib.constant.Constant;
import com.mwx.baselib.net.AppConfig;
import com.mwx.baselib.net.BaseResponses;
import com.mwx.baselib.net.DefaultObserver;
import com.mwx.baselib.net.NetworkManager;
import com.mwx.baselib.utils.GlideUtils;
import com.mwx.baselib.utils.SPUtils;
import com.zhanbo.zhanbowymusic.R;
import com.zhanbo.zhanbowymusic.databinding.FragmentPrivateRoamingBinding;
import com.zhanbo.zhanbowymusic.music.MusicController;
import com.zhanbo.zhanbowymusic.music.OnEvent;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * 私人漫游
 */
public class PrivateRoamingFragment extends BaseFragment<FragmentPrivateRoamingBinding> {


    private List<SongBean> songs  =  new ArrayList<>();
    private int currentPosition;

    public PrivateRoamingFragment() {
    }

    public static PrivateRoamingFragment newInstance() {
        PrivateRoamingFragment fragment = new PrivateRoamingFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }



    @Override
    protected Object getLayout() {
        return R.layout.fragment_private_roaming;
    }

    @Override
    protected void init(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
//从Controller类中获取歌曲数据
//        List<SongBean> sheetData = MusicController.getInstance().getSheetData();
//        songs.clear();
//        songs.addAll(sheetData);
//        //如果当前页面歌曲存在，更新第一次显示的歌曲信息
//        if (songs.size() > 0) {
//
//            currentPosition = MusicController.getInstance().getCurrentPosition();
//            if (currentPosition < songs.size()) {
//                binding.tvName.setText(songs.get(currentPosition).getName());
//                binding.tvArtistName.setText("歌手：" + songs.get(currentPosition).getArtistName());
//                binding.tvAlbumName.setText("专辑：" + songs.get(currentPosition).getAlbumName());
//                GlideUtils.loadImage(_mActivity,songs.get(currentPosition).getCoverImg(),binding.ivHead);
//            }
//        }
    }


    @Override
    public void onHiddenChanged(boolean hidden) {
        if (!hidden){
            initData();
            updateProgress();
        }
        super.onHiddenChanged(hidden);
    }

    @Override
    public void onResume() {
        super.onResume();

    }



    @Override
    public void onLazyInitView(@Nullable Bundle savedInstanceState) {
        super.onLazyInitView(savedInstanceState);


    }
    private static final int INTERNAL_TIME = 500;// 音乐进度间隔时间
    // 每间隔1s通知更新进度
    private void updateProgress() {
        // 使用Handler每间隔1s发送一次空消息，通知进度条更新
        mHandler.sendEmptyMessageDelayed(1, INTERNAL_TIME);
    }
    /**
     * 进度条定时器
     */
    private Handler mHandler = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message msg) {
            if (msg.what == 1) {
                // 展示给进度条和当前时间
                int currentDuration = MusicController.getInstance().getCurrentDuration();
                binding.lrcView.updateTime(currentDuration);
//                currentTime.setText(TimeUtil.parseTime(currentDuration));
                //Log.e("进度条定时器", "");
                // 继续定时发送数据
                updateProgress();

            }
            return true;

        }
    });
    @Override
    protected void initData() {

        NetworkManager.getInstance().getApiService().playListList(45)
                .subscribeOn(Schedulers.io())
                .compose(this.bindToLifecycle())
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new DefaultObserver<BaseResponses<List<PlayListSongBean>>>(_mActivity) {
                    @Override
                    public void onSuccess(BaseResponses<List<PlayListSongBean>> listBaseResponses) {
                        List<SongBean> songBeans = new ArrayList<>();
                        for (PlayListSongBean datum : listBaseResponses.getData()) {
                            songBeans.add(datum.getSong());
                        }
                        songs= songBeans;
                        MusicController.getInstance().updateAndPlaySheetData(songBeans);

                        binding.tvName.setText(songs.get(MusicController.getInstance().getCurrentPosition()).getName());
                        binding.tvArtistName.setText("歌手：" + songs.get(MusicController.getInstance().getCurrentPosition()).getArtistName());
                        binding.tvAlbumName.setText("专辑：" + songs.get(MusicController.getInstance().getCurrentPosition()).getAlbumName());
                        GlideUtils.loadImage(_mActivity, songs.get(MusicController.getInstance().getCurrentPosition()).getCoverImg(), binding.ivHead);
                    }
                });
    }
    @Subscribe
    public void getEvent(OnEvent event) {

        if (event.id==3) {
            currentPosition = MusicController.getInstance().getCurrentPosition();
            if (currentPosition < songs.size()) {
                binding.tvName.setText(songs.get(currentPosition).getName());
                binding.tvArtistName.setText("歌手：" + songs.get(currentPosition).getArtistName());
                binding.tvAlbumName.setText("专辑：" + songs.get(currentPosition).getAlbumName());
                GlideUtils.loadImage(_mActivity, songs.get(currentPosition).getCoverImg(), binding.ivHead);

                //进度条总长度
//                int dt = MusicController.getInstance().getDuration();
//                binding.seekbar.setMax(dt);
                initLyric();
            }

        }


    }
    private void initLyric() {
        binding.lrcView.loadLrcByUrl(AppConfig.URL+songs.get(currentPosition).getLyricUrl());
    }
    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}